<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
 * 控制器创建: php artisan make:controller Admin/CommonController
 */
class CommonController extends Controller
{
    protected $HrefId,$adminRules;
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $adminid = $request->user()->id;
            $action = \Route::current()->getActionName();
            list($class, $method) = explode('@', $action);
            $class = substr(strrchr($class,'\\'),1);
            $class = explode('Controller',$class);
            $class = strtolower($class[0]);
            $method = strtolower($method);
            if($adminid != 1){
                $rules = DB::table('admins')
                    ->leftJoin('auth_group', 'admins.group_id', '=', 'auth_group.group_id')
                    ->where('admins.id',$adminid)
                    ->select('auth_group.rules')
                    ->first();
                $rules = json_decode(json_encode($rules),true);
                $this->adminRules = explode(',',$rules['rules']);
                $rule = DB::table('auth_rule')->where('href',$class.'/'.$method)->value('id');
                $this->HrefId = $rule;
                if($this->HrefId || ($class.'/'.$method != 'index/index')){
                    if(!in_array($this->HrefId,$this->adminRules)){
                        dd(50);
                    }
                }
            }
            $data['configinfo'] = configs();
            view()->share('infomsg',$data);
            return $next($request);
        });
    }
}
